/**
 * 
 */
package com.mc.wordpdf;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.stream.Collectors;

import javax.imageio.ImageIO;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.math3.analysis.function.Constant;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ResourceUtils;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.Pictures;
import com.mc.parameter.Parameter;

import cn.hutool.core.io.file.FileReader;

/**
 * poi-tl（poi template language）是Word模板引擎，
 * 使用模板和数据创建很棒的Word文档。
 * 
 * @author w.weizhen
 *
 */
public class PoiTlWordToPDF {
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		
		wirte();
		
		
		//System.out.println(random());
		
	}
	
	//当前模块,数据和文件存放路径
	private static final String path = Parameter.data_path + "poitl" + File.separator;
	private static final String fileName = "fxcjcbl_template";
	private static final String expand = ".docx";
	private static final FileReader fileReader = new FileReader(path + "test.txt");
	
	private static final List<String> lines = fileReader.readLines();
	
	private static void wirte()
	{
		try {
			Map<String, Object> data = new HashMap<String, Object>()
	        {
	            {
	                put("bh", lines.get(random()));
	                put("dsr", lines.get(random()));
	                put("dz1", lines.get(random()));
	                put("lxdh1", lines.get(random()));
	                put("fddbr", lines.get(random()));
	                put("lxdh2", lines.get(random()));
	                
	                StringBuffer jdyj = new StringBuffer();
	                for (int i = 0; i < 100; i++) 
	                {
	                	jdyj.append(lines.get(random()));
					}
	                put("jdyj", jdyj.toString());
	            	
	            	put("hiOrgName", lines.get(random()));
	                put("rq", lines.get(random()));
	                put("hiOrgSignFile", Pictures.ofLocal(path + "123456.png").size(120, 120).create());
	                
	            	
	            	File file1 = new File(path + "b9bb4f7dc16a4b9498362cead541eeac.png");
	            	File file2 = new File(path + "513e9113968c486dbab2bca5d966b958.png");
	            	
	                BufferedImage image1 = ImageIO.read(file1);
	                BufferedImage image2 = ImageIO.read(file2);
	            	
	                int width = 100;
	            	int x1 = image1.getWidth()/width;
	            	int x2 = image2.getWidth()/width;
	            	
	            	put("hiSignFile1", Pictures.ofStream(FileUtils.openInputStream(file1)).size(image1.getWidth()/x1, image1.getHeight()/x1).create());
	                put("hiSignFile2", Pictures.ofStream(FileUtils.openInputStream(file2)).size(image2.getWidth()/x2, image2.getHeight()/x2).create());
	                
	                put("foot", lines.get(random()));
	            }
	        };
	        
	        // 模版文件路径
	        File tplFile = new File(path + fileName + expand);
	        // 生成数据文件,word文件
	        File docFile = new File(path + fileName + new Random().nextInt(100) + expand);
	        
	        XWPFTemplate template = XWPFTemplate.compile(tplFile).render(data);
	        template.writeAndClose(Files.newOutputStream(docFile.toPath()));
	        
	        
	        
	        
	        
	        
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	
	private static Integer random()
	{
		Random random = new Random();
		return random.nextInt(lines.size());
	}
	
}